<?php
namespace app\admin\controller;
class Config extends Base{
    public function __construct() {
        parent::__construct();
    }

    public function index(){
        if(request()->isPost()) {
            $data = input();
            $this->_saveConfig($data);
            return returnJson(0,'保存成功');
        }
        return view();
    }
    
    protected function _saveConfig ($data) {
        foreach ($data as $k=>$v) {
            $where = array(
                'key' => $k
            );
            $res = db('config')->where($where)->find();
            if($res) {
                $save = [
                    'value' => $v
                ];
                db('config')->where($where)->update($save);
            }
        }
    }
    
    public function qiniu() {
        if(request()->isPost()) {
            $data = input();
            $this->_saveConfig($data);
            return returnJson(0,'保存成功');
        }
        return view();
    }

    public function swiper () {
        $datalist = db('swiper')->order('ord desc,id desc')->select();
        $this->assign('datalist',$datalist);
        return view();
    }
    
    public function addswiper () {
        $id = input('id');
        if($id) {
            $swiper = db('swiper')->where('id',$id)->find();
            $this->assign('swiper',$swiper);
        }
        if(request()->isPost()) {
            if(!input('imgurl')) {
                return returnJson(1, '请上传图片');
            }
            $data = input();
            if($id) {
                $res = db('swiper')->where('id',$data['id'])->find();
                if(!$res) {
                    return returnJson(1,'error');
                }
                db('swiper')->where('id',$id)->update($data);
                return returnJson(0,'修改成功');
            }else{
                $data['create_time'] = date('Y-m-d H:i:s');
                db('swiper')->insert($data);
                return returnJson(0,'添加成功');
            }
        }
        $this->view->engine->layout('common/login-layout');
        return view();
    }
    
    public function deleteswiper () {
        if(!request()->isAjax()){
            return returnJson(1,'error');
        }
        $id = input('id');
        if(!$id) {
            return returnJon(1,'error');
        }
        $swiper = db('swiper')->where('id',$id)->find();
        if(!$swiper){
            return returnJson(1,'error');
        }
        $res = db('swiper')->where(['id'=>$id])->delete();
        if($res) {
            return returnJson(0,'删除成功');
        }
        return returnJson(1,'删除失败');
    }

    public function run () {
        if(request()->isPost()) {
            $data = input();
            $paylist = [];
            foreach($data['pay_text'] as $k=>$v) {
                if(!$data['pay_text'][$k] || !is_numeric($data['pay_value'][$k])){
                    continue;
                }
                if($data['pay_value'][$k]<0) {
                    continue;
                }
                $paylist[] = array(
                    'text' => $data['pay_text'][$k],
                    'value' => $data['pay_value'][$k]
                );
            }
            unset($data['pay_text']);
            unset($data['pay_value']);
            $data['run_paylist'] = json_encode($paylist);
            $this->_saveConfig($data);
            return returnJson(0,'保存成功');
        }
        return view();
    }

    public function uploadpic() {
        require_once  VENDOR_PATH.'qiniu/autoload.php';
         // 用于签名的公钥和私钥
        $accessKey = config('qiniu_accesskey');
        $secretKey = config('qiniu_secretkey');
        $bucket = config('qiniu_bucket');
        $domain = config('qiniu_domain');
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        // 上传到七牛后保存的文件名
        $key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        // 要上传的空间
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new \Qiniu\Storage\UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            var_dump($err);
        } else {
            return returnJson(0, '上传成功',$domain.$ret['key']);
        }
    }

    public function wxapp(){
        if(request()->isPost()) {
            $data = input();
            $this->_saveConfig($data);
            return returnJson(0,'保存成功');
        }
        return view();
    }
}
